package o;

import android.net.TrafficStats;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import com.huawei.hms.framework.network.restclient.hwhttp.route.IPRoute;

class bmv {
    private int ʻ;
    private int ʼ;
    private int ʽ;
    private long ˊ = -1;
    private bmw ˊॱ = bmw.ॱ;
    private long ˋ = -1;
    private bmw ˋॱ;
    private bmy ˎ;
    private HandlerThread ˏ;
    private int ˏॱ;
    private double ͺ = 0.0d;
    private e ॱ;
    private int ॱˊ = 1;
    private boolean ॱˋ = false;
    private int ॱॱ = 0;
    private boolean ॱᐝ = false;
    private int ᐝ;

    class e extends Handler {
        final /* synthetic */ bmv ˊ;

        e(bmv o_bmv, Looper looper) {
            this.ˊ = o_bmv;
            super(looper);
        }

        public void handleMessage(Message message) {
            bmr.ॱ.ˎ("NetworkQualitySampler", "handleMessage");
            switch (message.what) {
                case 1:
                    this.ˊ.ॱॱ();
                    if (this.ˊ.ॱˋ) {
                        sendEmptyMessageDelayed(1, (long) this.ˊ.ˏॱ);
                        return;
                    } else {
                        ((HandlerThread) Thread.currentThread()).quitSafely();
                        return;
                    }
                default:
                    throw new IllegalArgumentException("Unknown what=" + message.what);
            }
        }

        void ˋ() {
            sendEmptyMessageDelayed(1, (long) this.ˊ.ˏॱ);
        }
    }

    bmv() {
    }

    void ˎ(bmy o_bmy, bmx o_bmx) {
        bmr.ॱ.ˎ("NetworkQualitySampler", "network quality sampling start.");
        if (o_bmy != null) {
            bmr.ॱ.ˎ("NetworkQualitySampler", "NetworkQualityListener registered!");
            this.ˎ = o_bmy;
        }
        ˎ(o_bmx);
        ˊ();
    }

    private void ˎ(bmx o_bmx) {
        int i = IPRoute.INTERVAL;
        if (o_bmx == null) {
            this.ʽ = IPRoute.INTERVAL;
            this.ᐝ = 5;
            this.ˏॱ = 1000;
        } else {
            if (o_bmx.ˎ() != 0) {
                i = o_bmx.ˎ();
            }
            this.ʽ = i;
            if (o_bmx.ˊ() != 0) {
                i = o_bmx.ˊ();
            } else {
                i = 5;
            }
            this.ᐝ = i;
            if (o_bmx.ॱ() != 0) {
                i = o_bmx.ॱ();
            } else {
                i = 1000;
            }
            this.ˏॱ = i;
        }
        this.ʼ = 10000 / this.ˏॱ;
        this.ʻ = (int) (((double) this.ʽ) * 0.5d);
        bmr.ॱ.ˋ("NetworkQualitySampler", "param initialized: speedBound=" + this.ʽ + ", stabilisationLimit=" + this.ᐝ + ", samplingGap=" + this.ˏॱ + ", invalidCountLimit=" + this.ʼ);
    }

    void ॱ() {
        if (this.ॱˋ) {
            ˏ();
            bmr.ॱ.ˎ("NetworkQualitySampler", "network quality sampling stopped by caller.");
        }
    }

    bmw ˋ() {
        return this.ˊॱ;
    }

    double ˎ() {
        return this.ͺ;
    }

    private void ˊ() {
        this.ॱˋ = true;
        bmp.ॱ();
        this.ˏ = new HandlerThread("NetworkQualityThread");
        this.ˏ.start();
        this.ॱ = new e(this, this.ˏ.getLooper());
        this.ॱ.ˋ();
        this.ˊ = TrafficStats.getTotalRxBytes();
        this.ˋ = SystemClock.elapsedRealtime();
    }

    private void ˏ() {
        this.ॱˋ = false;
        this.ॱˊ = 0;
        this.ˊॱ = bmw.ॱ;
        this.ˋॱ = null;
        this.ॱᐝ = false;
        this.ॱॱ = 0;
    }

    private void ʽ() {
        if (this.ॱˋ && this.ˎ != null) {
            this.ˎ.ˎ(this.ˊॱ);
        }
    }

    private void ॱॱ() {
        double ʻ = ʻ();
        if (ʻ < 0.0d) {
            bmr.ॱ.ˎ("NetworkQualitySampler", "averageSpeed invalid, ignored.");
            if (this.ॱॱ >= this.ʼ) {
                bmr.ॱ.ˋ("NetworkQualitySampler", "invalidCount = " + this.ॱॱ + ", too many, network poor");
                this.ˊॱ = bmw.POOR;
                this.ॱॱ = 0;
                ʽ();
                return;
            }
            this.ॱॱ++;
            return;
        }
        bmw o_bmw = ʻ > ((double) this.ʽ) ? bmw.GOOD : bmw.POOR;
        bmr.ॱ.ˎ("NetworkQualitySampler", "temp quality = " + o_bmw.toString());
        if (this.ॱᐝ) {
            if (o_bmw != this.ˋॱ) {
                bmr.ॱ.ˎ("NetworkQualitySampler", "quality changed again, suspicious abandoned.");
                this.ॱᐝ = false;
                this.ॱˊ = 1;
            } else if (this.ॱˊ < this.ᐝ || !ˋ(ʻ)) {
                bmr.ॱ.ˎ("NetworkQualitySampler", "suspicious value stable counter:" + this.ॱˊ);
                this.ॱˊ++;
            } else {
                bmr.ॱ.ˎ("NetworkQualitySampler", "suspicious confirmed");
                this.ॱᐝ = false;
                this.ॱˊ = 1;
                this.ॱॱ = 0;
                this.ˊॱ = this.ˋॱ;
                ʽ();
                this.ʻ = (int) (((double) this.ʽ) * 0.1d);
            }
        } else if (o_bmw != this.ˊॱ) {
            bmr.ॱ.ˎ("NetworkQualitySampler", "find suspicious");
            this.ˋॱ = o_bmw;
            this.ॱᐝ = true;
        }
    }

    private double ʻ() {
        double d = -1.0d;
        long totalRxBytes = TrafficStats.getTotalRxBytes();
        long elapsedRealtime = SystemClock.elapsedRealtime();
        if (ˊ((double) (totalRxBytes - this.ˊ), (double) (elapsedRealtime - this.ˋ))) {
            d = bmp.ˋ(((double) (totalRxBytes - this.ˊ)) / ((double) (elapsedRealtime - this.ˋ)));
        }
        this.ˊ = totalRxBytes;
        this.ˋ = elapsedRealtime;
        return d;
    }

    private boolean ˊ(double d, double d2) {
        this.ͺ = d / d2;
        if (d2 > 0.0d && this.ͺ >= ((double) this.ʻ)) {
            return true;
        }
        return false;
    }

    private boolean ˋ(double d) {
        if (this.ˊॱ == bmw.ॱ) {
            return true;
        }
        if (this.ˊॱ == bmw.POOR && d > ((double) this.ʽ) * 1.2d) {
            return true;
        }
        if (this.ˊॱ != bmw.GOOD || d >= ((double) this.ʽ) * 0.8d) {
            return false;
        }
        return true;
    }
}
